package com.app.tablet.view;


import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.app.tablet.R;

public class AlarmSetActivity extends BaseActivity implements OnClickListener{
	
	private static final String TagProject = "Tablet-Project";
	private static final String TagPage = "AlarmSetActivity-Page ::";
	
	private Button leftBtn;
	private Button rightBtn;
	private TextView titletxt;
	private TimePicker timepick;
	private RelativeLayout soundrl;
	private int hour;
	private int min;
	private int mHour;
	private int mMin;
	private int mSec;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_alarmsetpage);
		setHeaderContent();

		soundrl = (RelativeLayout)findViewById(R.id.rl_sound);
		timepick = (TimePicker)findViewById(R.id.timepicker);
		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);
		soundrl.setOnClickListener(this);
	}

	private void setHeaderContent() {
		leftBtn = (Button) findViewById(R.id.leftHeaderBtn);
		leftBtn.setText(R.string.cancelBtn);

		rightBtn = (Button) findViewById(R.id.rightHeaderBtn);
		rightBtn.setText(R.string.doneBtn);
		
		titletxt =(TextView)findViewById(R.id.titleHeaderTxt);
		titletxt.setText(R.string.titleSetalarmpage);
	}
	
	public void settime(){
		if (AddPageActivity.dbData.getAlarmtime()!=null){
			String s = AddPageActivity.dbData.getAlarmtime();
			String[] sspace = s.split(" ");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftHeaderBtn:
			this.finish();
			break;
		case R.id.rightHeaderBtn:
			hour = timepick.getCurrentHour();
			min = timepick.getCurrentMinute();
			Calendar cal = Calendar.getInstance();
			Date date = cal.getTime();
			mHour = date.getHours();
			mMin = date.getMinutes();
			mSec = date.getSeconds();
			int diff=((((hour * 60)+min)*60)-(((mHour * 60)+mMin)*60+mSec));
			Log.i(TagProject, TagPage+"Timepicker time : "+hour+", "+min);
			Log.i(TagProject, TagPage+"Device time : "+mHour+", "+mMin+", "+mSec);
			Log.i(TagProject, TagPage+"Difference time : "+diff);
			if (diff>0){
				AddPageActivity.dbData.setAlarmtimeINsec(diff);				
			}else if(diff<0){
				diff = (86400+diff);
				Log.i(TagProject, TagPage+"Difference time : "+diff);
				AddPageActivity.dbData.setAlarmtimeINsec(diff);
			}
			String time = twelveHour(hour);
			AddPageActivity.dbData.setAlarmtime(time);
			AddPageActivity.dbData.setTones("Default Tone");
			this.finish();
			break;
		}
	}
	
	String minute="";
	private String twelveHour(int c){
		if (min<10){
			minute="0"+min;
		}else{
			minute=String.valueOf(min);
		}
		if(c>12){
	        return String.valueOf(c-12)+ ":"+(String.valueOf(minute)+" PM");
	    }else if(c==12){
	        return String.valueOf(c)+ ":"+(String.valueOf(minute)+" PM");
	    }else if(c==0){
	        return String.valueOf(12)+ ":"+(String.valueOf(minute)+" AM");
	    }else{
	        return "0"+String.valueOf(c)+ ":"+(String.valueOf(minute)+" AM");
	    }
	}

}